﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using MySql.Data.MySqlClient;
using System.Data;
using l1cs.server.utils;
using l1cs.server.datatables;

namespace l1cs.server.serverpackets
{
    public class S_CharacterConfig : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_CharacterConfig ).FullName );

        private byte[] _byte = null;

        public S_CharacterConfig( int objectId )
        {
            buildPacket( objectId );
        }

        private void buildPacket( int objectId )
        {
            int length = 0;
            byte[] data = null;
            String sqlstr = "SELECT * FROM character_config WHERE object_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = objectId;
                DataTable dt = new DataTable();
                foreach ( DataRow item in wrapper.GetDataTable( cmd ).Rows )
                {
                    length = ((byte[]) item["data"] ).Length ;
                    data = Encoding.GetEncoding( Config.CLIENT_LANGUAGE_CODE ).
                        GetBytes( Convert.ToString( item["data"] ) );
                }
            }
            if ( length != 0 )
            {
                writeC( Opcodes.S_OPCODE_SKILLICONGFX );
                writeC( 41 );
                writeD( length );
                writeByte( data );
            }
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = getBytes();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_CharacterConfig";
        }
    }
}